package cn.edu.dlut.port.isp.controller;

import java.util.ArrayList;
import java.util.Calendar;
import java.util.Date;
import java.util.HashMap;
import java.util.Iterator;
import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.ModelAndView;
import org.springframework.web.servlet.mvc.support.RedirectAttributes;

import cn.edu.dlut.port.isp.bean.User;
import cn.edu.dlut.port.isp.repostitory.Berth_infoRepository;
import cn.edu.dlut.port.isp.repostitory.Channel_infoRepository;
import cn.edu.dlut.port.isp.repostitory.CompanyRepository;
import cn.edu.dlut.port.isp.repostitory.M_thp_compRepository;
import cn.edu.dlut.port.isp.repostitory.M_thp_dlpRepository;
import cn.edu.dlut.port.isp.repostitory.Pipeline_infoRepository;
import cn.edu.dlut.port.isp.repostitory.Port_infoRepository;
import cn.edu.dlut.port.isp.repostitory.Railway_infoRepository;
import cn.edu.dlut.port.isp.repostitory.UserRepository;
import cn.edu.dlut.port.isp.service.PortService;

@Controller // This means that this class is a Controller
// @RequestMapping(path = "/demo") // This means URL's start with /demo (after
// Application path)
public class MainController {
	@Autowired // This means to get the bean called userRepository
				// Which is auto-generated by Spring, we will use it to handle the data
	private UserRepository userRepository;
	@Autowired
	private Port_infoRepository port_infoRepository;
	@Autowired
	private Railway_infoRepository railway_infoRepository;
	@Autowired
	private Channel_infoRepository channel_infoRepository;
	@Autowired
	private CompanyRepository companyRepository;
	
	@Autowired
	private Berth_infoRepository berth_infoRepository;
	
	@Autowired
	private Pipeline_infoRepository pipeline_infoRepository;
	@Autowired
	private PortService portService;	
	@Autowired
	private M_thp_dlpRepository m_thp_dlpRepository;
	@Autowired
	private M_thp_compRepository m_thp_compRepository;

	@RequestMapping("/port")
	public ModelAndView port() {
		ModelAndView mav=new ModelAndView("port");
//		Iterable<User> list=userRepository.findAll();
		mav.addObject("list1",port_infoRepository.findAll());//获取所有港区信息
		mav.addObject("list2",railway_infoRepository.findAll());//获取所有铁路信息
		mav.addObject("list3",portService.getChannelList());//航道信息
		mav.addObject("list4",portService.getAnchorageList());//锚地信息
		mav.addObject("list5",companyRepository.findAll());
		Calendar date = Calendar.getInstance();
	    String year = String.valueOf(date.get(Calendar.YEAR));
		
	    mav.addObject("list6",m_thp_dlpRepository.findAll());//获取所有的大连港吞吐量数据
		mav.addObject("list61",portService.getDlpThpByYear(new Integer(year)-1));//获取当年的大连港吞吐量数据
		mav.addObject("list62",portService.getAllDlpThpYear());
		mav.addObject("list63",portService.getAllCompThpDate());
		mav.addObject("list64",portService.getAllCompThpYear());
		
		
		mav.addObject("list7",port_infoRepository.findAll());
		mav.addObject("list8",pipeline_infoRepository.findAll());
		return mav;
		//return "register";
	}
	
	@RequestMapping(path = "/getThpByCompanyAndYear.json")
	public @ResponseBody Map<String,Object> getThpByCompany(@RequestParam String comp_id,@RequestParam Integer year) {
		Map<String,Object> map=new HashMap<String,Object>();
		if(year==0)//默认当前年份
		{
		Calendar date = Calendar.getInstance();
	    String str_year = String.valueOf(date.get(Calendar.YEAR));
	    year=Integer.parseInt(str_year);
		}
	    
		map.put("list62", portService.getThpByCompanyAndYear(comp_id,new Integer(year)));
		return map;

	}

	@RequestMapping(path = "/getCompThpByYear.json")
	public @ResponseBody Map<String,Object> getCompThpByYear(@RequestParam Integer year) {
		Map<String,Object> map=new HashMap<String,Object>();
		map.put("list64", portService.getCompThpByYear(year));
		return map;		
	}
	
	
	@RequestMapping(path = "/getDlpThpByYear.json")
	public @ResponseBody Map<String,Object> getDlpThpByYear(@RequestParam Integer year) {
		Map<String,Object> map=new HashMap<String,Object>();
		map.put("list61",portService.getDlpThpByYear(year));
		return map;		
	}


	@GetMapping(path = "/add") // Map ONLY GET Requests
	public @ResponseBody String addNewUser(@RequestParam String name, @RequestParam String email) {
		// @ResponseBody means the returned String is the response, not a view name
		// @RequestParam means it is a parameter from the GET or POST request

		User n = new User();
		n.setName(name);
		n.setEmail(email);
		n.setBerthdate(new Date());
		userRepository.save(n);
		return "Saved";
	}

	@GetMapping(path = "/all")
	public @ResponseBody Iterable<User> getAllUsers() {
		// This returns a JSON or XML with the users
		return userRepository.findAll();
	}

	@RequestMapping(path = "/list")
	public String getUserList(@RequestParam(defaultValue = "tangguolei") String search, Model model) {
		Iterable<User> users = userRepository.findAll();
		List<User> schUsers = new ArrayList<>();
		Iterator<User> userItr = users.iterator();
		while (userItr.hasNext()) {
			User user = userItr.next();
			System.out.println(user.getName() + "\t" + search);
			if (user.getName().toLowerCase().equals(search.trim().toLowerCase())) {
				schUsers.add(user);
			}
		}
		model.addAttribute("message", "Smart Port Users");
		model.addAttribute("users", schUsers);
		return "results";
	}

	@RequestMapping(value = "/llist", method = RequestMethod.POST)
	public String limitedList(HttpServletRequest request, RedirectAttributes redirectAttributes) {
		String search = request.getParameter("search");
		if (search.toLowerCase().contains("guoguo")) {
			redirectAttributes.addFlashAttribute("error", "Try using spring instead!");
			return "redirect:/";
		}
		redirectAttributes.addAttribute("search", search);
		return "redirect:list";
	}
}
